package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.constant.enums.CorFileTypeEnum;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.common.security.service.TdfUser;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.supervision.convert.JzdxLegalInstrumentConvert;
import com.engine4cloud.tdf.supervision.dto.create.JzdxLegalInstrumentCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.JzdxFileQueryDTO;
import com.engine4cloud.tdf.supervision.dto.query.JzdxLegalInstrumentQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxLegalInstrumentResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.JzdxLegalInstrumentUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.JzdxLegalInstrumentEntity;
import com.engine4cloud.tdf.supervision.mapper.JzdxLegalInstrumentMapper;
import com.engine4cloud.tdf.supervision.service.JzdxFileService;
import com.engine4cloud.tdf.supervision.service.JzdxLegalInstrumentService;
import com.engine4cloud.tdf.supervision.service.JzdxService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * 法律文书
 *
 * @author wenjie
 * @date 2024-04-01 14:23:35
 */
@Service
public class JzdxLegalInstrumentServiceImpl extends ServiceImpl<JzdxLegalInstrumentMapper, JzdxLegalInstrumentEntity> implements JzdxLegalInstrumentService {
    @Resource
    private JzdxService jzdxService;

    @Resource
    private JzdxFileService fileService;

    @Override
    public PageInfo<JzdxLegalInstrumentResultDTO> pageList(Params pageable, JzdxLegalInstrumentQueryDTO dto) {
        IPage<JzdxLegalInstrumentResultDTO> page = this.baseMapper.pageList(Pages.page(pageable), dto);
        return Pages.convert(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean create(JzdxLegalInstrumentCreateDTO dto) {
        JzdxResultDTO dx = Optional.ofNullable(jzdxService.getByCode(dto.getRybh()))
                .orElseThrow(() -> new ValidateCodeException("矫正对象数据不存在"));
        TdfUser user = SecurityUtils.getUser();

        JzdxLegalInstrumentEntity entity = JzdxLegalInstrumentConvert.INSTANCE.create(dto, dx.getTenantId());
        super.save(entity);
        //设置法律文书
        this.saveInstrumentList(dto, entity, user);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(JzdxLegalInstrumentUpdateDTO dto) {
        JzdxLegalInstrumentEntity old = Optional.ofNullable(super.getById(dto.getId()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        TdfUser user = SecurityUtils.getUser();

        super.updateById(JzdxLegalInstrumentConvert.INSTANCE.update(dto));
        //设置法律文书
        this.updateInstrumentList(dto, old, user);
        return true;
    }

    @Override
    public JzdxLegalInstrumentResultDTO getById(Long id) {
        JzdxLegalInstrumentResultDTO result = Optional.ofNullable(JzdxLegalInstrumentConvert.INSTANCE.convert(super.getById(id)))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        //设置法律文书信息
        this.setInstrumentList(result);
        return result;
    }

    private void saveInstrumentList(JzdxLegalInstrumentCreateDTO dto, JzdxLegalInstrumentEntity entity, TdfUser user) {
        fileService.batchSaveFile(new JzdxFileQueryDTO(entity.getRybh(), entity.getLcbh(), CorFileTypeEnum.CORRECTOR_LEGAL_INSTRUMENT), dto.getLegalInstrumentList(), user);
    }

    public void updateInstrumentList(JzdxLegalInstrumentUpdateDTO dto, JzdxLegalInstrumentEntity entity, TdfUser user) {
        fileService.batchUpdateDetailFile(new JzdxFileQueryDTO(entity.getRybh(), entity.getLcbh(), CorFileTypeEnum.CORRECTOR_LEGAL_INSTRUMENT), dto.getLegalInstrumentList(), user);
    }

    private void setInstrumentList(JzdxLegalInstrumentResultDTO dto) {
        JzdxFileQueryDTO query = new JzdxFileQueryDTO(dto.getRybh(), dto.getLcbh(), CorFileTypeEnum.CORRECTOR_LEGAL_INSTRUMENT);
        dto.setLegalInstrumentList(fileService.findList(query));
    }

}